#!/bin/bash

buildAll() {
  #docker login reg.domain.com
  #docker builder prune
  cd ../ || exit
  ver="latest"

  # build api images
  dirs=("admin" "game" "business" "pay")
  for dir in "${dirs[@]}"
  do
    pwd
    echo buid "${dir}"
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker buildx build --platform=linux/amd64 -f app/api/"${dir}"/Dockerfile -t "${imgName}"-api:${ver} .
  done

  # build rpc images
  dirs2=("game" "wallet" "usercenter" "pay" "stat")
  for dir in "${dirs2[@]}"
  do
    pwd
    echo buid "${dir}"
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker buildx build --platform=linux/amd64 -f app/rpc/"${dir}"/Dockerfile -t "${imgName}"-rpc:${ver} .
  done

  # build mq images
  dirs3=("usercenter")
  for dir in "${dirs3[@]}"
  do
    pwd
    echo buid "${dir}"
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker buildx build --platform=linux/amd64 -f app/mq/"${dir}"/Dockerfile -t "${imgName}"-mq:${ver} .
  done

  # build timer images
  dirs4=("job" "scheduler")
  for dir in "${dirs4[@]}"
  do
    pwd
    echo buid "${dir}"
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker buildx build --platform=linux/amd64 -f app/timer/"${dir}"/Dockerfile -t "${imgName}"-timer:${ver} .
  done

  # package images
  pwd
  docker save -o ./images/navi-mumbai-${ver}.tar admin-api:latest game-api:latest business-api:latest pay-api:latest game-rpc:latest wallet-rpc:latest usercenter-rpc:latest pay-rpc:latest stat-rpc:latest usercenter-mq:latest job-timer:latest scheduler-timer:latest

  # remove images
  for dir in "${dirs[@]}"
  do
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker rmi "${imgName}"-api:${ver}
  done

  # remove images
  for dir in "${dirs2[@]}"
  do
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker rmi "${imgName}"-rpc:${ver}
  done

  # remove images
  for dir in "${dirs3[@]}"
  do
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker rmi "${imgName}"-mq:${ver}
  done

  # remove images
  for dir in "${dirs4[@]}"
  do
    imgName=$(echo "${dir}" | sed 's/\//\-/g')
    docker rmi "${imgName}"-timer:${ver}
  done

  return
}

buildApiImage() {
  #docker builder prune
  #docker login reg.domain.com
  cd ../ || exit
  pwd
  ldir=$1
  echo buid "${ldir}"
  ver="latest"
  pwd
  imgName=$(echo "${ldir}" | sed 's/\//\-/g')
  docker buildx build --platform=linux/amd64 -f app/api/"${ldir}"/Dockerfile -t "${imgName}"-api:${ver} .
  docker save -o ./images/"${imgName}"-api-${ver}.tar "${imgName}"-api:${ver}
  docker rmi "${imgName}"-api:${ver}
  return
}

buildRpcImage() {
  #docker builder prune
  #docker login reg.domain.com
  cd ../ || exit
  pwd
  ldir=$1
  echo buid "${ldir}"
  ver="latest"
  pwd
  imgName=$(echo "${ldir}" | sed 's/\//\-/g')
  docker buildx build --platform=linux/amd64 -f app/rpc/"${ldir}"/Dockerfile -t "${imgName}"-rpc:${ver} .
  docker save -o ./images/"${imgName}"-rpc-${ver}.tar "${imgName}"-rpc:${ver}
  docker rmi "${imgName}"-rpc:${ver}
  return
}

buildMqImage() {
  #docker builder prune
  #docker login reg.domain.com
  cd ../ || exit
  pwd
  ldir=$1
  echo buid "${ldir}"
  ver="latest"
  pwd
  imgName=$(echo "${ldir}" | sed 's/\//\-/g')
  docker buildx build --platform=linux/amd64 -f app/mq/"${ldir}"/Dockerfile -t "${imgName}"-mq:${ver} .
  docker save -o ./images/"${imgName}"-mq-${ver}.tar "${imgName}"-mq:${ver}
  docker rmi "${imgName}"-mq:${ver}
  return
}

buildTimerImage() {
  #docker builder prune
  #docker login reg.domain.com
  cd ../ || exit
  pwd
  ldir=$1
  echo buid "${ldir}"
  ver="latest"
  pwd
  imgName=$(echo "${ldir}" | sed 's/\//\-/g')
  docker buildx build --platform=linux/amd64 -f app/timer/"${ldir}"/Dockerfile -t "${imgName}":${ver} .
  docker save -o ./images/"${imgName}"-timer-${ver}.tar "${imgName}":${ver}
  docker rmi "${imgName}":${ver}
  return
}